﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Coronox.SkyShell
{
    internal class IniFile
    {
        private string m_Path;


        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool WritePrivateProfileString(string lpAppName,
           string lpKeyName, string lpString, string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern uint GetPrivateProfileString(
           string section, string key, string def,
           StringBuilder returnedString, uint nSize, string filePath);


        internal IniFile(string strIniPath)
        {
            m_Path = strIniPath;
        }

        internal string Path
        {
            get { return m_Path; }
        }

        internal bool IniWriteValue(string Section, string Key, string Value)
        {
            bool Return;
            Return = WritePrivateProfileString(Section,
                               Key,
                               Value,
                               m_Path);
            return Return;
        }


        internal bool IniDeleteValue(string Section, string Key)
        {
            bool Return;
            Return = WritePrivateProfileString(Section,
                               Key,
                               null,
                               m_Path);
            return Return;
        }

        internal string IniReadValue(string Section, string Key)
        {
            StringBuilder strResultValue = new StringBuilder(1024);
            uint res = GetPrivateProfileString(Section, Key, "", strResultValue, (uint)1024, m_Path);
            if (res > 1024-2)
                throw new Exception(String.Format("Error in GetPrivateProfileString with error code {0}, Path: {1}", res, m_Path));
            return strResultValue.ToString();
        }
    }

}
